home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / misc / math / MathFX_src.lha / fxcntr.c < prev    next >
C/C++ Source or Header  |  1995-12-20  |  6KB  |  211 lines

  1. /* Contour "ny" by "nx" real array "points" at the level "zlev" */
  2. /* points is a pointer to a 2d array of nx by ny points. */
  3. /* iscan has nx elements. ixstor and iystor each have nstor elements. */
  4.  
  5. #include "mathfx.h"
  6.  
  7.  
  8.  
  9. void fxcntr(points,nx,ny,kx,lx,ky,ly,zlev,iscan,ixstor,iystor,nstor,tr)
  10. int nx, ny, ky, ly, kx, lx, nstor;
  11. float zlev, *points;
  12. int iscan[], ixstor[], iystor[];
  13.  
  14. void (*tr)();
  15. {
  16.       int kcol, krow, kstor, kscan, iwbeg, ixbeg, iybeg, izbeg;
  17.       int iboun, iw, ix, iy, iz, ifirst, istep, ixgo, iygo;
  18.       int l, ixg, iyg, ia, ib, ixt, iyt, jstor, next;
  19.       float dist, dx, dy, xnew, ynew, x, y;
  20.       float xlas=0., ylas=0., tpx, tpy, xt, yt;
  21.  
  22.       /* Initialize memory pointers */
  23.  
  24.       kstor = 0;
  25.       kscan = 0;
  26.  
  27.       for (krow=ky; krow<=ly; krow++) {
  28.         for (kcol=kx+1; kcol <= lx; kcol++)  {
  29.  
  30.           /* Check if a contour has been crossed */
  31.  
  32.           x = *(points + (kcol-2)*ny + krow-1);
  33.           y = *(points + (kcol-1)*ny + krow-1);
  34.           if (x < zlev && y >= zlev) {
  35.             ixbeg = kcol-1;
  36.             iwbeg = kcol;
  37.           }
  38.           else if (y < zlev && x >= zlev) {
  39.             ixbeg = kcol;
  40.             iwbeg = kcol-1;
  41.           }
  42.           else
  43.             goto lab70;
  44.  
  45.           iybeg = krow;
  46.           izbeg = krow;
  47.  
  48.           /* Yes, a contour has been crossed. Check to see if it */
  49.           /* is a new one. */
  50.  
  51.           for(l=0;l<kscan;l++)
  52.             if (ixbeg == iscan[l]) goto lab70;
  53.  
  54.           /* Here is the section which follows and draws a contour */
  55.  
  56.           for (iboun=1; iboun>= -1; iboun -= 2) {
  57.  
  58.             /* Set up starting point and initial search directions */
  59.  
  60.             ix = ixbeg;
  61.             iy = iybeg;
  62.             iw = iwbeg;
  63.             iz = izbeg;
  64.             ifirst = 1;
  65.             istep = 0;
  66.             ixgo = iw - ix;
  67.             iygo = iz - iy;
  68.  
  69. lab20:
  70.               fxccal(points,nx,ny,zlev,ix,iy,ixgo,iygo,&dist);
  71.               dx = dist * ixgo;
  72.               dy = dist * iygo;
  73.               xnew = ix+dx;
  74.               ynew = iy+dy;
  75.  
  76.              /* Has a step occured in search? */
  77.  
  78.               if (istep != 0) {
  79.                 if (ixgo*iygo == 0) {
  80.  
  81.                   /* This was a diagonal step, so interpolate missed */
  82.                   /* point, rotating 45 degrees to get it */
  83.  
  84.                   ixg = ixgo;
  85.                   iyg = iygo;
  86.                   fxr45(&ixg,&iyg,iboun);
  87.                   ia = iw-ixg;
  88.                   ib = iz-iyg;
  89.                   fxccal(points,nx,ny,zlev,ia,ib,ixg,iyg,&dist);
  90.                   (*tr)(xlas,ylas,&tpx,&tpy);
  91.                   drawor(tpx,tpy);
  92.                   dx = dist*ixg;
  93.                   dy = dist*iyg;
  94.                   xlas = ia+dx;
  95.                   ylas = ib+dy;
  96.                 }
  97.                 else {
  98.                   if (dist > 0.5) {
  99.                     xt = xlas;
  100.                     xlas = xnew;
  101.                     xnew = xt;
  102.                     yt = ylas;
  103.                     ylas = ynew;
  104.                     ynew = yt;
  105.                   }
  106.                 }
  107.               }
  108.               if (ifirst != 1) {
  109.                 (*tr)(xlas,ylas,&tpx,&tpy);
  110.                 drawor(tpx,tpy);
  111.               }
  112.               else {
  113.                 (*tr)(xnew,ynew,&tpx,&tpy);
  114.                 movwor(tpx,tpy);
  115.               }
  116.               xlas = xnew;
  117.               ylas = ynew;
  118.  
  119.               /* Check if the contour is closed */
  120.  
  121.               if (ifirst != 1 && ix == ixbeg && iy == iybeg
  122.                   && iw == iwbeg && iz == izbeg) {
  123.                 (*tr)(xlas,ylas,&tpx,&tpy);
  124.                 drawor(tpx,tpy);
  125.                 goto lab70;
  126.               }
  127.               ifirst = 0;
  128.  
  129.               /* Now the rotation */
  130.  
  131.               istep = 0;
  132.               fxr45(&ixgo,&iygo,iboun);
  133.               iw = ix+ixgo;
  134.               iz = iy+iygo;
  135.  
  136.               /* Check if out of bounds */
  137.  
  138.               if (iw<kx || iw>lx || iz<ky || iz>ly) goto  lab50;
  139.  
  140.               /* Has contact been lost with the contour? */
  141.  
  142.               if (*(points+(iw-1)*ny+iz-1) < zlev) {
  143.  
  144.                 /* Yes, lost contact => step to new centre */
  145.  
  146.                 istep = 1;
  147.                 ix = iw;
  148.                 iy = iz;
  149.                 fxr135(&ixgo,&iygo,iboun);
  150.                 iw = ix+ixgo;
  151.                 iz = iy+iygo;
  152.  
  153.                 /* And do the contour memory */
  154.  
  155.                 if (iy == krow) {
  156.                   kscan = kscan+1;
  157.                   iscan[kscan-1] = ix;
  158.                 }
  159.                 else if (iy>krow) {
  160.                   kstor = kstor+1;
  161.                   if (kstor>nstor) {
  162.                     fatal("Heap exhausted in FXCONT.");
  163.                     goto lab70;
  164.                   }
  165.                   ixstor[kstor-1] = ix;
  166.                   iystor[kstor-1] = iy;
  167.                 }
  168.               }
  169.             goto lab20;
  170. lab50:
  171.             /* Reach here only if boundary encountered - Draw last bit */
  172.  
  173.             (*tr)(xnew,ynew,&tpx,&tpy);
  174.             drawor(tpx,tpy);
  175.           }
  176. lab70:
  177.           ;    /* Null statement to carry label */
  178.         }
  179.  
  180.         /* Search of row comfxete - set up memory of next row in iscan and */
  181.         /* edit ixstor and iystor */
  182.  
  183.         if (krow<ny) {
  184.           jstor = 0;
  185.           kscan = 0;
  186.           next = krow+1;
  187.           for (l=1; l<=kstor; l++) {
  188.             ixt = ixstor[l-1];
  189.             iyt = iystor[l-1];
  190.  
  191.             /* Memory of next row into iscan */
  192.  
  193.             if (iyt == next) {
  194.               kscan = kscan+1;
  195.               iscan[kscan-1] = ixt;
  196.       
  197.             /* Retain memory of rows to come, and forget rest */
  198.  
  199.             }
  200.             else if (iyt>next) {
  201.               jstor = jstor+1;
  202.               ixstor[jstor-1] = ixt;
  203.               iystor[jstor-1] = iyt;
  204.             }
  205.           }
  206.           kstor = jstor;
  207.         }
  208.       }
  209. }
  210.  
  211.